Queues: First-In, First-Out (FIFO) Structure
A Queue is a linear structure designed for sequential processing, adhering strictly to the FIFO principle: the element that has been in the queue the longest is the next to be removed.
Unlike Stacks (which use a single access point, the Top), Queues require two pointers:
- Enqueue (Insertion): Elements are added to the Rear (or back).
- Dequeue (Deletion): Elements are removed from the Front.
This structure guarantees that the order of processing strictly matches the order of arrival, essential for scheduling and buffering applications.
Queue Operation Complexity
| Operation | Description | Time $T(N)$ | Space $S(N)$ |
|---|---|---|---|
| Enqueue | Insert at rear | $O(1)$ | $O(1)$ |
| Dequeue | Remove from front | $O(1)$ | $O(1)$ |
| Peek | View front element | $O(1)$ | $O(1)$ |
| Search | Find element | $O(N)$ | $O(1)$ |
Note: $O(1)$ complexity is maintained by using a Linked List or a Circular Array implementation.
1. A queue holds [A, B, C] (front to rear). 'D' is Enqueued, then one element is Dequeued. What is the final state?
2. What is the time complexity of the `Enqueue` operation in an efficiently implemented queue?
3. Which graph traversal algorithm fundamentally relies on a queue?
4. Why can a simple array be inefficient for a queue's `Dequeue` operation?